package com.imessage.action.api.virtual;

import com.bean.BaseConfig;
import com.bean.BusinessTaskDetail;
import com.bean.DeviceVirtual;
import com.imessage.action.api.BaseAction;
import com.imessage.jackson.JsonObject;
import com.imessage.type.BaseConfigCode;
import com.imessage.type.BusinessTaskCmd;
import com.imessage.type.BusinessTaskState;
import com.imessage.utils.DateUtil;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class ApiVirtualBaseAction extends BaseAction {


    protected BusinessTaskDetail getRunBusinessTaskDetail(DeviceVirtual deviceVirtual){
        if(deviceVirtual==null){
            return null;
        }
        if(deviceVirtual.getTaskState().equals(BusinessTaskState.run)){
            List<Expression> expressionList=new ArrayList<Expression>();
            expressionList.add(Expr.eq("state", deviceVirtual.getTaskState().getCode()));
            expressionList.add(Expr.eq("cmd", deviceVirtual.getTaskCmd().getCode()));
            expressionList.add(Expr.eq("DeviceVirtual", deviceVirtual));
            return baseService.findObject(BusinessTaskDetail.class, expressionList, "", "id desc");
        }
        BusinessTaskDetail businessTaskDetail=null;
        if(deviceVirtual.getTaskCmd().equals(BusinessTaskCmd.start_virtual)){
            businessTaskDetail= taskService.createWumaConfig(deviceVirtual);
        }else if (deviceVirtual.getTaskCmd().equals(BusinessTaskCmd.replace_wuma)){
            businessTaskDetail= taskService.createSingleMsg(deviceVirtual);
        } else if(deviceVirtual.getTaskCmd().equals(BusinessTaskCmd.send_single_msg)){
            long maxSilenceTime=50*60*60;
            BaseConfig baseConfig=configService.findObject(BaseConfigCode.max_silence_time);
            if(StringUtils.isNotBlank(baseConfig.getValue())){
                try{
                    maxSilenceTime=Long.valueOf(baseConfig.getValue());
                }catch (Exception e){
                    log.error("", e);
                }
            }
            double currSilenceTime=DateUtil.minuSecond(deviceVirtual.getUpdatedTime(), new Date());
            if(currSilenceTime>=maxSilenceTime){
                businessTaskDetail=taskService.createBatchMsg(deviceVirtual);
            }else{
                return null;
            }
        }
        return businessTaskDetail;
    }


    protected void initGetObject(
            JsonObject dataObject,BusinessTaskDetail businessTaskDetail){
        dataObject.put("task_id", businessTaskDetail.getId()+"");
        dataObject.put("task_cmd", businessTaskDetail.getCmd().getCode());
        dataObject.put("msg_type", businessTaskDetail.getType().getCode());
        JsonObject addrObject=new JsonObject();
        addrObject.put("user", businessTaskDetail.getUsername());
        addrObject.put("passwd", businessTaskDetail.getPasswd());
        addrObject.put("down_url",businessTaskDetail.getDownFileAddr()+ File.separator+businessTaskDetail.getZipFileName());
        addrObject.put("upload_url", businessTaskDetail.getUploadFileAddr());
        addrObject.put("protocol", "scp");
        dataObject.put("file_addr", addrObject);
        dataObject.put("file_name", businessTaskDetail.getZipFileName());
    }

}
